package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.Domain.Device;
import com.ruoyi.StarStudio.Mapper.DeviceMapper;
import com.ruoyi.StarStudio.Mapper.ZoneMapper;
import com.ruoyi.StarStudio.Service.DeviceService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private ZoneMapper zoneMapper;

    @Autowired
    protected Validator validator;

    private static final Logger log = LoggerFactory.getLogger(DeviceServiceImpl.class);

    @Override
    public List<Device> getAll() {
        return deviceMapper.getAll();
    }

    @Override
    public Device getById(int deviceId) {
        return deviceMapper.getById(deviceId);
    }

    @Override
    public void add(Device device, int zoneId) {
        String zoneNameByZoneId = zoneMapper.getZoneNameByZoneId(zoneId);
        device.setDevice_location(zoneNameByZoneId);
        deviceMapper.add(device);
        deviceMapper.addToZone_Device(zoneId, device.getDevice_id());
    }

    @Override
    public void update(Device device) {
        deviceMapper.update(device);
    }

    @Override
    public void delete(int deviceId) {
        deviceMapper.delete(deviceId);
    }

    @Override
    public List<Device> getList(Device device) {
        return deviceMapper.getList(device);
    }

    /**
     * 从Excel表格导入设备数据
     * @param deviceList
     * @param isUpdateSupport
     * @return
     */
    @Override
    public String importDevice(List<Device> deviceList, boolean isUpdateSupport) {
        if (StringUtils.isNull(deviceList) || deviceList.size() == 0) {
            throw new ServiceException("导入设备数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Device device : deviceList) {
            try {
                Device deviceBySeriesNumber = deviceMapper.getBySeriesNumber(device.getDevice_series_number());
                if (StringUtils.isNull(deviceBySeriesNumber)) {
                    BeanValidators.validateWithException(validator, device);
                    deviceMapper.add(device);
                    int zoneIdByZoneName = zoneMapper.getZoneIdByZoneName(device.getDevice_location());
                    //向中间表插入数据  此时 deviceID已有数据
                    deviceMapper.addToZone_Device(zoneIdByZoneName,device.getDevice_id());
                    successNum++;
                    successMsg.append("<br/>" + successNum + ".设备_" + device.getDevice_name() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, device);
                    deviceMapper.update(device);
                    successNum++;
                    successMsg.append("<br/>" + successNum + ".设备_" + device.getDevice_name() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ".设备_" + device.getDevice_name() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + ".设备_" + device.getDevice_name() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


}